{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wikipedia Agent\n",
    "\n",
    "In the [Wikipedia Search Tools](https://github.com/ag2ai/ag2/blob/main/notebook/tools_wikipedia_search.ipynb) notebook, we demonstrated how to create Agents with basic Wikipedia search capabilities.\n",
    "\n",
    "Now, we're taking it a step further with `WikipediaAgent`—a powerful agent equipped with built-in wiki search tools right out of the box!"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Package Installation\n",
    "\n",
    "\n",
    "To get started with the Wikipedia search integration in AG2, follow these steps:\n",
    "\n",
    "Install AG2 with `\"wikipedia` and `openai` since we use OpenAI's LLMs in our example:\n",
    "\n",
    "```bash\n",
    "pip install -U \"ag2[wikipedia, openai]\"\n",
    "```\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:\n",
    "> ```bash\n",
    "> pip install -U \"autogen[wikipedia, openai]\"\n",
    "> ```\n",
    "> or\n",
    "> ```bash\n",
    "> pip install -U \"ag2[wikipedia, openai]\"\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.\n",
    "\n",
    "\n",
    "You're all set! Now you can start using Wikipedia Search with AG2."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Demonstration\n",
    "The `WikipediaAgent` comes packaged with two Wikipedia tools, `WikipediaQueryRunTool` for running title/keyword searches and `WikipediaPageLoadTool` for fetching full page content. Together with the fact that it has a pre-configured system message (which you can override), you only need to create the agent and add it to a chat to get going."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import LLMConfig\n",
    "from autogen.agents.experimental import WikipediaAgent"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agent Configuration\n",
    "\n",
    "Configure `WikipediaAgent` and run it. Using the agent's `run` command will automatically "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# LLM configuration\n",
    "config_list = LLMConfig(config_list={\"api_type\": \"openai\", \"model\": \"gpt-5-nano\"})\n",
    "\n",
    "# Create the agent\n",
    "wiki_agent = WikipediaAgent(name=\"wiki-agent\", llm_config=config_list)\n",
    "\n",
    "# If you are adding this agent into a chat that requires separate tool execution, register the tools with the executing agent:\n",
    "# for tool in wiki_agent.tools:\n",
    "# tool.register_for_execution(the_executing_agent)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start the Conversation\n",
    "\n",
    "With the setup complete, you can now use the conversation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = wiki_agent.run(\n",
    "    message=\"What's the population of Australia?\",\n",
    "    max_turns=2,\n",
    "    # Instruct the chat to associate the WikipediaAgent's tools with the internal tool executor\n",
    "    tools=wiki_agent.tools,\n",
    ")\n",
    "\n",
    "# Run the chat\n",
    "response.process()"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Search Wikipedia with WikipediaAgent",
   "tags": [
    "tools",
    "wikipedia",
    "search"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
